package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.entry.YddAdvimg;
import com.itheima.ydd.entry.YddLink;
import com.itheima.ydd.service.YddLinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ydd_link")
@Slf4j
public class YddLinkController {
    @Autowired
    private YddLinkService yddLinkService;

    @GetMapping("/list")
    public R Page(int type,int page,int limit){
        Page<YddLink> linkPage = new Page<>(page, limit);
        LambdaQueryWrapper<YddLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddLink::getType,type);
        yddLinkService.page(linkPage, queryWrapper);


        Map map = new HashMap<>();

        map.put("count",linkPage.countId());
        map.put("totalPages",linkPage.getTotal());
        map.put("pageSize",linkPage.getMaxLimit());
        map.put("currentPage",linkPage.getCurrent());
        map.put("data",linkPage.getRecords());

        return R.success(map);
    }
    //新增
    @PostMapping("/add")
    public R save(@RequestBody YddLink yddLink) {
        YddLink link = new YddLink();
        BeanUtils.copyProperties(yddLink,link);
        link.setDefault_data(yddLink.getType());
        yddLinkService.save(link);

        return R.success("添加成功");

    }
    //修改
    @GetMapping("/rowInfo")
    public R rowInfo(Integer id){
        YddLink link = yddLinkService.getById(id);

        return R.success(link);
    }
    //修改
    @PostMapping("update")
    public R update(@RequestBody YddLink yddLink){
        yddLinkService.updateById(yddLink);
        return R.success("修改成功");
    }

    //删除数据
    @GetMapping("/del")
    public R delete(Long id){
        yddLinkService.removeById(id);
        return R.success("删除成功");
    }
}
